package ipmsg.network;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.SocketException;

import ipmsg.etc.Command;
import ipmsg.etc.GlobalVar;

public class MsgDaemonServer extends Thread {
	public void run() {
		try {
			byte[] buf = new byte[1500];
			DatagramPacket recv = new DatagramPacket(buf, buf.length);

			for (;;) {
				UtilityNet.msgSocket.receive(recv);

				if (!GlobalVar.BLACK_LIST.contains(recv.getAddress().getHostAddress())) {

					byte[] buf2 = new byte[recv.getLength()];
					System.arraycopy(recv.getData(), 0, buf2, 0, recv.getLength());
					Command tmpCom = Command.createCommand(buf2, recv.getAddress().getHostAddress());
					if (tmpCom != null) {
						GlobalVar.COMMAND_QUEUE_EMPTY.acquire();
						GlobalVar.pushCommand(tmpCom);
						GlobalVar.COMMAND_QUEUE_fULL.release();
					} // end if
				} // end if
			} // end for
		} catch (SocketException ex) {
		} catch (IOException ex) {
		} catch (InterruptedException ex) {
		}
	}
}
